package lib

import (
	"GameServer/Common/data"
	"GameServer/Game/errors"
	"GameServer/Game/m"
	"GameServer/Game/sp"
	"sort"

	log "github.com/sirupsen/logrus"
)

// 处理新兵特训状态
func CalcRecruitTrain(uid string, level int64, star int64, monster int64) {
	rt := m.GetRecruitTrain(uid)

	tpl, ok := data.GetAllDatas("RecruitTrains")
	if !ok {
		log.WithFields(log.Fields{
			"uid":    uid,
			"method": "CalcRecruitTrain",
		}).Error("没有找到新兵特训配置,RecruitTrains")
		return
	}
	for _, item := range tpl {
		itemT := item.(data.RecruitTrain)
		id := int64(itemT.Id)
		val, ok := rt.State[id]
		if !ok {
			rt.State[id] = &m.RTState{Tid: id}
			val, _ = rt.State[id]
		}
		if val.LevelState == 0 && level >= int64(itemT.Level) {
			val.LevelState = 1
		}
		if val.StarState == 0 && star >= int64(itemT.Star) {
			val.StarState = 1
		}
		if val.MonsterState == 0 && (monster == int64(itemT.Monster) || int64(itemT.Monster) == 0) {
			val.MonsterState = 1
		}
		// 三个状态达成
		if val.MonsterState == 1 && val.LevelState == 1 && val.StarState == 1 && val.State == 0 {
			val.State = 1
		}
	}
	rt.Update(rt)
}

// 获取新兵特训状态
func GetRecruitTrain(u *m.User) (r []*m.RTState, err error) {
	r = make([]*m.RTState, 0)

	rt := m.GetRecruitTrain(u.Uid.Hex())
	if rt.Finsh == 1 { // 所有任务都完成了，并且奖励领取了
		return
	}
	monster := int64(0)
	if u.MaxPos.LockBlockTid > 7 {
		monster = int64(240101)
	}
	CalcRecruitTrain(u.Uid.Hex(), u.MaxPos.LockBlockTid, u.StarSum, monster)
	rt = m.GetRecruitTrain(u.Uid.Hex())

	flag := true
	for _, item := range rt.State {
		if item.State < 2 {
			flag = false
		}
		r = append(r, item)
	}
	if flag { // 为真，表示所有奖励都被领取了
		rt.Finsh = 1
		rt.Update(rt)
		return
	}
	sort.Slice(r, func(i, j int) bool {
		return r[i].Tid < r[j].Tid
	})

	return r, nil
}

// 领取新兵特训奖励
func RecruitTrainReward(uid string, tid int64) (r []m.BaseKV, err error) {
	rt := m.GetRecruitTrain(uid)
	val, ok := rt.State[tid]
	if !ok {
		err = errors.NewErrcode(data.Errcode.ServerErr, "没有该任务")
		log.WithFields(log.Fields{
			"uid":    uid,
			"tid":    tid,
			"method": "RecruitTrainReward",
		}).Error("没有该任务")
		return
	}
	if val.State != 1 {
		err = errors.NewErrcode(data.Errcode.ServerErr, "该任务没有完成，或者已经领取过奖励")
		log.WithFields(log.Fields{
			"uid":    uid,
			"tid":    tid,
			"State":  val.State,
			"method": "RecruitTrainReward",
		}).Error("该任务没有完成，或者已经领取过奖励")
		return
	}
	tpl, ok := data.Get("RecruitTrains", uint64(tid))
	if !ok {
		err = errors.NewErrcode(data.Errcode.ConfigNotFound, "找不到配置表，RecruitTrains")
		log.WithFields(log.Fields{
			"uid":    uid,
			"method": "RecruitTrainReward",
		}).Error("没有找到新兵特训配置,RecruitTrains")
		return
	}
	tplT := tpl.(data.RecruitTrain)
	r = make([]m.BaseKV, 0)
	for _, item := range tplT.Reward {
		r = append(r, m.BaseKV{
			Key: int64(item.Key),
			Val: int64(item.Value),
		})
	}
	val.State = 2
	rt.Update(rt)
	return
}

// 获取新兵特训完成的状态
func GetRTStateFinshed(uid string) (r *m.RTState) {
	rt := m.GetRecruitTrain(uid)
	lst := make([]*m.RTState, 0)
	for _, item := range rt.State {
		lst = append(lst, item)
	}
	sort.Slice(lst, func(i, j int) bool {
		return lst[i].Tid < lst[j].Tid
	})
	for _, item := range lst {
		if item.State == 1 {
			return item
		}
	}
	return nil
}

// 新兵特训消息处理
func RecruitTrainMsg(uid string, level int64, star int64, monster int64, PSend func(interface{}) error) {
	if PSend == nil {
		return
	}
	CalcRecruitTrain(uid, level, star, monster)
	rts := GetRTStateFinshed(uid)
	if rts == nil {
		return
	}

	PSend(&sp.RecruitTrainNoticeRequest{
		Lst: FormatRTState(rts),
	})
}

// 重置新兵特训
func ResetRecruitTrain(u *m.User) {
	rt := m.GetRecruitTrain(u.Uid.Hex())
	rt.Finsh = 0
	rt.State = make(map[int64]*m.RTState)
	rt.Update(rt)
}
